package com.spzx.product.receiver;

import com.rabbitmq.client.AMQP;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.product.service.IProductService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;

import java.io.IOException;


@Slf4j
@Component
public class ProductReceiver {
    @Resource
    IProductService productService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PRODUCT,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_UNLOCK,durable = "ture"),
            key = {MqConst.ROUTING_UNLOCK}
    ))
    public void a(Channel channel, Message message, String orderNo) throws IOException {
        System.out.println("库存释放消费："+ orderNo);
        productService.unlock(orderNo);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PRODUCT, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_MINUS, durable = "true"),
            key = {MqConst.QUEUE_MINUS}
    ))
    public void b(Channel channel, Message message, String orderNo) throws IOException {
        System.out.println("库存扣减消费：" + orderNo);
        productService.minus(orderNo);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }

}
